package com.neu.wms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.wms.dto.RoleDto;
import com.neu.wms.dto.RoleDtoFengSir;
import com.neu.wms.entity.SysMenu;
import com.neu.wms.entity.SysRole;
import com.neu.wms.util.entity.SysRole;
import com.neu.wms.service.RoleService;
import com.neu.wms.service.MenuService;
import com.neu.wms.util.ResultVo;
import com.neu.wms.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author ：fengSir
 * @date ：Created By 2023-06-27 14:44
 * @description ：TODO
 */
@RestController
@RequestMapping("/roles")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @GetMapping("{uid}")
    public ResultVo queryAll(@PathVariable Integer uid){
        List<SysRole> roles = roleService.selectAll();
        Integer[] checkList = roleService.selectRoleIdByUid(uid);
        return ResultVo.ok().data("roleList",roles).data("checkList",checkList);
    }



    @GetMapping("queryMenuNameByName/{uid}")
    public ResultVo queryMenuNameByName(@PathVariable Integer uid){
        List<SysMenu> menus = menuService. queryMenu();

        Integer[] checkList = menuService.selectMenuIdByUid(uid);
        System.out.println("checkList:"+ Arrays.toString(checkList));
        System.out.println("uid:"+uid);
        return ResultVo.ok().data("menuList",menus).data("checkList",checkList);
    }


    @GetMapping("queryById/{uid}")
    public ResultVo queryById(@PathVariable Integer uid){
        SysRole roles = roleService.queryById(uid);

        return ResultVo.ok().data("role",roles);
    }

    @PutMapping("update")
    public ResultVo update(@RequestBody SysRole role) {


        int i = roleService.updateByPrimaryKeySelective(role);
        if (i > 0) {
            return ResultVo.ok("更新成功");
        } else {
            return ResultVo.error("更新失败");
        }
    }


    @PutMapping
    public ResultVo assignRole(@RequestBody RoleDtoFengSir roleDto){
        System.out.println(roleDto);
        roleService.insertRole(roleDto);
        return ResultVo.ok("授权成功");
    }

    @PostMapping("")
    public ResultVo add(@RequestBody SysRole role) {

        System.out.println(role);



        int i = roleService.insertSelective(role);
        if (i > 0) {
            return ResultVo.ok("添加成功");
        } else {
            return ResultVo.error("添加失败");
        }
    }

    @PostMapping("queryAll")
    public ResultVo queryAll(@RequestBody RoleDto roleDto) {
        PageHelper.startPage(roleDto.getCurrentPage(), roleDto.getPageSize(), true);
        List<RoleVo> roles = roleService.selectAll(roleDto);
        PageInfo<RoleVo> pageInfo = new PageInfo<>(roles);
        return ResultVo.ok().data("list", roles).data("total", pageInfo.getTotal());
    }


    @DeleteMapping("{id}")
    public ResultVo delete(@PathVariable("id") Long id) {
        int i = roleService.deleteByPrimaryKey(id);
        if (i > 0) {
            return ResultVo.ok("删除成功");
        } else {
            return ResultVo.error("删除失败");
        }
    }
}
